package com.x.Part3_同步代码块处理线程安全;

/**
 * ClassName: WindowTest
 * Package: IntelliJ IDEA
 * Description:
 * Written by HanXu
 * Create 2024.03.25 22:53
 */
public class WindowTest {
    public static void main(String[] args) {

        Window window = new Window();

        new Thread(window).start();
        new Thread(window).start();
        new Thread(window).start();

    }
}

class Window implements Runnable{
    int ticket = 1000;

    Object o = new Object();

    @Override
    public void run() {
        while (true){
            //synchronized (this) {
            //synchronized (o) {
            synchronized (Window.class) {//反射类对象
                    if(ticket>0){
                        System.out.println(Thread.currentThread().getName()+"票号为：" + ticket);
                        ticket--;
                    }else
                        break;
                }
            }
        }
    }

